package com.itheima.sfbx.insurance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {


    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        try {
//            构建分页对象
            Page<Coefficent> coefficentPage = new Page<>(pageNum, pageSize);
//            构建查询条件
            LambdaQueryWrapper<Coefficent> queryWrapper = new LambdaQueryWrapper<>();
//            根据系数项编号
            queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKey()),
                    Coefficent::getCoefficentKey,coefficentVO.getCoefficentKey());
            queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKeyName()),
                    Coefficent::getCoefficentKeyName,coefficentVO.getCoefficentKeyName());
//            按创建时间降序
            queryWrapper.orderByDesc(Coefficent::getCreateTime);

//            执行分页查询
            Page<Coefficent> page = page(coefficentPage, queryWrapper);

//            将查询结果转化为coefficientVO格式并返回
            Page<CoefficentVO> coefficentVOPage = BeanConv.toPage(page, CoefficentVO.class);
            return coefficentVOPage;
        }catch (Exception e){
            log.error("分页查询异常: {}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.PAGE_FAIL);
        }
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        return null;
    }

    @Override
    public CoefficentVO save(CoefficentVO coefficentVO) {
        return null;
    }

    @Override
    public Boolean update(CoefficentVO coefficentVO) {
        return null;
    }

    @Override
    public Boolean delete(String[] checkedIds) {
        return null;
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        return null;
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        return null;
    }
}
